const resultHandler = {}
resultHandler.returnData = function (result){
    return {
        code:0,
        msg:'',
        data:result
    }
}
resultHandler.returnErr = function (err="server error",code=500){
    return {
        code,
        msg:err,
        data:''
    }
}

resultHandler.handleAsyncError = (handler)=>{
    return async (req,res,next)=>{
        try{
            const resp = await handler(req,res,next)
            res.send(resultHandler.returnData(resp))
        }catch(err){
            next(err)// 向错误处理中间件抛

        }
    }
}

module.exports = resultHandler